import pygame
from pygame.sprite import Sprite

# Add color definition
WHITE = (255, 255, 255)

class Alien(Sprite):
    def __init__(self, screen, speed_factor=1, image_path='images/alien.bmp', score_value=10):
        """初始化外星人并设置其初始位置"""
        super().__init__()
        self.screen = screen
        self.image = pygame.image.load(image_path)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # 将每个新外星人放在屏幕左上角附近
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # 存储外星人的准确位置
        self.x = float(self.rect.x)

        # 外星人移动速度
        self.speed_factor = speed_factor
        self.fleet_direction = 1  # 1表示向右移动，-1表示向左移动

        # 外星人得分
        self.score_value = score_value

    def update(self):
        """向左或向右移动外星人"""
        self.x += (self.speed_factor * self.fleet_direction)
        self.rect.x = self.x

    def check_edges(self):
        """如果外星人位于屏幕边缘，就返回True"""
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right or self.rect.left <= 0:
            return True

    def blitme(self):
        """在指定位置绘制外星人"""
        self.screen.blit(self.image, self.rect)

# 定义不同类型的外星人
class FastAlien(Alien):
    def __init__(self, screen):
        super().__init__(screen, speed_factor=2, image_path='images/fast_alien.bmp', score_value=20)

class StrongAlien(Alien):
    def __init__(self, screen):
        super().__init__(screen, speed_factor=1, image_path='images/strong_alien.bmp', score_value=30)

class ZigzagAlien(Alien):
    def __init__(self, screen):
        super().__init__(screen, speed_factor=1.5, image_path='images/zigzag_alien.bmp', score_value=25)
        self.zigzag_direction = 1

    def update(self):
        """以锯齿形移动外星人"""
        self.x += (self.speed_factor * self.fleet_direction)
        self.rect.x = self.x
        self.rect.y += self.zigzag_direction
        if self.rect.top <= 0 or self.rect.bottom >= self.screen.get_rect().bottom:
            self.zigzag_direction *= -1

def create_fleet(screen, aliens, speed_factor, level, mode):
    alien_types = [Alien, FastAlien, StrongAlien, ZigzagAlien]
    alien_width = Alien(screen).rect.width
    available_space_x = 800 - 2 * alien_width
    number_aliens_x = available_space_x // (2 * alien_width)

    for row_number in range(level):
        for alien_number in range(number_aliens_x):
            alien_type = alien_types[(row_number + alien_number) % len(alien_types)]
            alien = alien_type(screen)
            alien.rect.x = alien_width + 2 * alien_width * alien_number
            alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
            aliens.add(alien)

def update_screen(screen, ship, aliens, bullets, lasers, missiles, score, stats):
    screen.fill(WHITE)
    ship.blitme()
    aliens.draw(screen)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    for laser in lasers.sprites():
        laser.draw_laser()
    for missile in missiles.sprites():
        missile.draw_missile()

    # 显示其他信息
    # ...
    pygame.display.flip()

if stats.game_active:
    update_aliens(aliens, ship, stats)

update_screen(screen, ship, aliens, bullets, lasers, missiles, score, stats)
